/*
 * $Id: timer.cpp 408 2010-06-16 00:40:36Z cmtonkinson@gmail.com $
 *
 * This file is part of the Symphony project <http://code.google.com/p/symphonymud/>
 * Copyright 2005-2010 Chris Tonkinson <cmtonkinson@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <cstdlib>
#include "area.h"
#include "avatar.h"
#include "regex.h"
#include "timer.h"

#include "world.h"

/************************************************************ BASE CLASS ************************************************************/
Timer::Timer( void ) {
  enabled( true );
  when( time( NULL ) - 1 );
  lower( 60 );
  upper( 0 );
  times( -1 );
  name( "undefined" );
  return;
}

Timer::~Timer( void ) {
  data().clear();
  return;
}

void Timer::reset( void ) {
  time_t now = time( NULL );

  if ( upper() > lower() ) {
    when( now + ( rand() % (upper()-lower()) + lower() ) );
  } else {
    when( now + lower() );
  }

  return;
}

bool Timer::ready( void ) {
  return ( when() <= time( NULL ) );
}

/************************************************************ AUTOSAVE ************************************************************/
TmrAutosave::TmrAutosave( void ) {
  name( "Autosave" );
  lower( 900 );
  upper( 0 );
  reset();
  return;
}

TmrAutosave::~TmrAutosave( void ) {
  return;
}

bool TmrAutosave::fire( void ) {
  World::Instance().save();
  reset();
  return true;
}

/************************************************************ TICK ************************************************************/
TmrTick::TmrTick( void ) {
  name( "Tick" );
  lower( 60 );
  upper( 90 );
  reset();
  return;
}

TmrTick::~TmrTick( void ) {
  return;
}

bool TmrTick::fire( void ) {
  World::Instance().bigBrother( NULL, ADMIN_BIGBRO_EVENTS, "Tick!" );
  reset();
  return true;
}

/************************************************************ Area Reset Timer ************************************************************/
TmrReset::TmrReset( Area* area ): Timer() {
  lower( 300 );
  upper( 600 );
  this->area( area );
  reset();
  return;
}

TmrReset::~TmrReset( void ) {
  return;
}

std::string TmrReset::name( void ) const {
  char buffer[MAX_BUFFER];
  sprintf( buffer, "Area %lu Reset (%s)", area()->ID(), area()->name().c_str() );
  return buffer;
}

bool TmrReset::fire( void ) {
  area()->reset();
  World::Instance().bigBrother( NULL, ADMIN_BIGBRO_RESETS, "Area Reset: %lu (%s)\n", area()->ID(), area()->name().c_str() );
  reset();
  return true;
}
